<?php

?>
<div class="hidden" id="addCnc-cont">
    <form class="well form-inline margin-horisontal login" action="" method="POST" name="addCncForm" id="addCncForm">
        <fieldset>

            <input type="hidden" name="action" id="action" value="<?php echo ACTIONS::VIEW_ADD_CNC; ?>">
            <input type="hidden" name="cnc_id" value="<?php echo Arr::getValue($params, 'cnc_id', 0); ?>">

            <div class="control-group" id="cg-cnc_name">
                <label class="control-label" for="cnc_name">Название</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" id="cnc_name" name="cnc_name" value="<?php echo Arr::getValue($params, 'cnc_name', ''); ?>">
                </div>
                <span class="help-inline"></span>
            </div>

            <div class="control-group" id="cg-cnc_parent_id">
                <label class="control-label" for="cnc_parent_id">Вложена</label>
                <div class="controls">
                    <select id="cnc_parent_id" name="cnc_parent_id" class="input-xlarge">
                        <?php
                        $selvalue = Arr::getValue($params, 'cnc_parent_id', 0);
                        $all = ConceptionsCollection::allRowsById();
                        $all[0] = array('id'=>0,'name'=>'Верхний уровень');
                        foreach ($all as $row) {
                            $key = Arr::getValue($row, 'id', 0);
                            $val = Arr::getValue($row, 'name', '???');
                            if (Arr::getValue($row, 'parent_id', 0)) continue;
                            $sel = ($key == $selvalue)? 'selected="selected"' : '';
                            $dis = $key? '' : ' style="color:darkblue;" ';
                            echo '<option value="'.$key.'" '.$sel.$dis.'>'.$val.'</option>';
                        }
                        ?>
                    </select>
                </div>
                <span class="help-inline"></span>
            </div>

            <div class="control-group" id="cg-cnc_priority">
                <label class="control-label" for="cnc_priority">Порядок отображения</label>
                <div class="controls">
                    <input type="text" class="input-large" id="cnc_priority" name="cnc_priority" value="<?php echo Arr::getValue($params, 'cnc_priority', ''); ?>">
                </div>
                <span class="help-inline"></span>
            </div>


            <div class="form-actions">
                <button type="submit" class="btn btn-primary" id="addCnc-submit"><i class="icon-ok icon-white"></i> Сохранить</button>
                <button type="reset" class="btn btn-danger" id="addCnc-cancel"><i class="icon-remove icon-white"></i> Отменить</button>
            </div>
        </fieldset>
    </form>
</div>
<script>

    function cncWrongFields(wf) {
        wf = wf || {};
        var wf_count = 0;

        var cgdivs = Elm.addCncForm.find('div.error').removeClass('error');
        cgdivs.find('span.help-inline').empty();

        for(var id in wf) {
            if (!wf.hasOwnProperty(id)) continue;
            var msg = wf[id];

            var cg = $('#cg-'+id).addClass('error');
            cg.find('span.help-inline').html(msg);
            wf_count++;
        }
        return wf_count;
    }



    $(function() {
        Elm.addCnc = $('#addCnc-cont');
        Elm.addCncForm = $('#addCncForm');
        Elm.addCncForm.successCallback = function(data) {};


        $('#addCnc-cancel').click(function(){
            Elm.addCnc.dialog('close');
            return false;
        });

        Elm.addCnc.dialog({
            width       : 350,
            modal       : true,
            resizable   : false,
            title       : "Добавить / изменить группу",
            autoOpen    : false,
            buttons     : {}
        }).removeClass('hidden');




        Elm.addCncForm.submit(function(){
            var send = Elm.addCncForm.serialize();
            cncWrongFields({});
            jsonRequest(
                'vwconceptions.php',
                send,

                function(data) {
                    cncWrongFields({});
                    if(checkSession(data)) {
                        if (!data.state) {
                            uiAlert(data.message,'Error');
                            return;
                        }

                        if (data.hasOwnProperty('response_data') && data.response_data) {
                            var resp = data.response_data;
                            if (resp.hasOwnProperty('wrongFields')) {
                                var wf = vwgWrongFields(resp.wrongFields);
                                if (wf*1>0) return;
                            }
                            if (resp.hasOwnProperty('insertedId')) {

                                if (
                                    Elm.addCncForm.hasOwnProperty('successCallback')
                                    && (typeof(Elm.addCncForm.successCallback) == 'function')
                                ) {
                                    var cdata;
                                    if (data.hasOwnProperty('request_data') && data.request_data) cdata = data.request_data;
                                    else cdata = {};
                                    $.extend(cdata, resp);

                                    if (cdata.cnc_parent_id*1<1) {
                                        $('#cnc_parent_id').append($('<option>').attr({value:cdata.insertedId}).html(cdata.cnc_name));
                                    }

                                    var cb = Elm.addCncForm.successCallback;
                                    Elm.addCncForm.successCallback = function() {};
                                    cb(cdata);
                                }
                            }
                        }
                        Elm.addCnc.dialog('close');
                        Elm.addCncForm.get(0).reset();
                    }
                    else Elm.loginFormModal.dialog('open');
                },

                function() {
                    cncWrongFields({});
                    uiAlert('Ошибка соединения','Error');
                }
            );
                return false;
        });

    });


</script>